package com.yunbao.hujin.mychoosealbum;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.yunbao.hujin.mychoosealbum.adapter.AdapterShowChooseImg;
import com.yunbao.hujin.mychoosealbum.utils.Const;
import com.yunbao.hujin.mychoosealbum.utils.GlideUtil;

import java.util.ArrayList;

/**
 * 点击图片后可以放大查看
 */
public class ChooseImgShowActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    private ArrayList<String> listPath;
    private int position;

    private RelativeLayout root;
    private ViewPager viewPager;
    private ImageView img;
    private ImageView back;

    private AdapterShowChooseImg adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 沉浸式状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        setContentView(R.layout.activity_choose_img_show);

        initData();//必须最前--否则listPath为空
        initView();
        bindView();
    }

    private void initView() {
        viewPager =  findViewById(R.id.view_pager);
        img =  findViewById(R.id.src_image);
        img.setVisibility(View.GONE);
        back = findViewById(R.id.back);
        root = findViewById(R.id.image_root);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
            }
        });
    }

    private void bindView() {
        viewPager.setPageMargin(15);
        adapter = new AdapterShowChooseImg(this, listPath, root);

        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(this);
        viewPager.setCurrentItem(position);
    }

    private void initData() {
        listPath = new ArrayList<>();
        if (ChooseImgAllActivity.instance != null) {
            listPath.addAll(ChooseImgAllActivity.instance.gridList);
        }
        position = getIntent().getIntExtra(Const.GOTO_FOR_SHOW_IMG_POSITION, 0);

        if (listPath.get(0).equals(Const.PHOTO)) {  //调用本地相机
            listPath.remove(0);
            position -= 1;
        }
    }

    /*************---ViewPager滑动监听--*************/
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        position = viewPager.getCurrentItem();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        setContentView(R.layout.view_null);
        GlideUtil.clearGlideMemory(this);
    }
}
